{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 输入文件分析和预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "compliance_policy_html_path = \"platform-policy-compliance/policy_html_files/us-amazon-compliance-policy.html\"\n",
    "\n",
    "prohibited_policy_html_path = \"platform-policy-compliance/policy_html_files/us-amazon-prohibited-policy.html\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已将 platform-policy-compliance/policy_html_files/us-amazon-compliance-policy.html 转换为 md/us-amazon-compliance-policy.md\n",
      "已将 platform-policy-compliance/policy_html_files/us-amazon-prohibited-policy.html 转换为 md/us-amazon-prohibited-policy.md\n"
     ]
    }
   ],
   "source": [
    "# 将html转成md文件\n",
    "import os\n",
    "import html2text\n",
    "\n",
    "def html_to_markdown(html_path, output_dir):\n",
    "    # 读取HTML文件\n",
    "    with open(html_path, 'r', encoding='utf-8') as file:\n",
    "        html_content = file.read()\n",
    "    \n",
    "    # 将HTML转换为Markdown\n",
    "    # 使用html2text将HTML转换为Markdown\n",
    "    h = html2text.HTML2Text()\n",
    "    h.ignore_links = False\n",
    "    h.ignore_images = False\n",
    "    h.ignore_emphasis = False\n",
    "    h.body_width = 0  # 禁用自动换行\n",
    "\n",
    "    markdown_content = h.handle(html_content)\n",
    "    # 处理特殊标题\n",
    "    import re\n",
    "    \n",
    "    def convert_to_h3(match):\n",
    "        return f\"### {match.group(1)}\"\n",
    "    \n",
    "    markdown_content = re.sub(r\"\\*\\*(.*?(?:permitted listings|prohibited listings))\\*\\*\", convert_to_h3, markdown_content)\n",
    "    \n",
    "    # 生成输出文件名\n",
    "    output_filename = os.path.splitext(os.path.basename(html_path))[0] + '.md'\n",
    "    output_path = os.path.join(output_dir, output_filename)\n",
    "    \n",
    "    # 将Markdown内容写入文件\n",
    "    with open(output_path, 'w', encoding='utf-8') as file:\n",
    "        file.write(markdown_content)\n",
    "    \n",
    "    print(f\"已将 {html_path} 转换为 {output_path}\")\n",
    "\n",
    "\n",
    "# 创建输出目录\n",
    "output_dir = \"md\"\n",
    "os.makedirs(output_dir, exist_ok=True)\n",
    "\n",
    "# 转换compliance policy\n",
    "html_to_markdown(compliance_policy_html_path, output_dir)\n",
    "\n",
    "# 转换prohibited policy\n",
    "html_to_markdown(prohibited_policy_html_path, output_dir)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
